package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.busi.DeviceCpeBusi;
import com.ncloud.resman.busi.DeviceLocationBusi;
import com.ncloud.resman.entity.DeviceLocation;
import com.ncloud.resman.service.DeviceLocationService;

import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ParseException;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by shiqiang on 2017/8/15.
 */
@Api(value = "自动化运维用户管理", description = "设备CPE信息", tags = "设备CPE信息",
        basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/resman/device")
public class DeviceCpeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DeviceCpeBusi deviceCpeBusi;
    @Autowired
    private DeviceLocationService deviceLocationService;
    @PostMapping(value = "/cpe")
    public Result<Object> saveCpe(HttpServletRequest request, @RequestBody JSONObject json) throws Exception {
        logger.info("Cpe报文信息："+json.toJSONString());
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.put("userID",Long.parseLong(userID.toString()));
        }
        deviceCpeBusi.saveCpe(json);
        return ResultUtil.success();
    }
    @GetMapping(value = "/cpe/state/{state}")
    public Result<?> getCpeList(@RequestParam(required=false) Long pageSize, @RequestParam(required=false) Long pageNum,
                                @RequestParam(required=false) Long deviceId, @PathVariable("state") String state){
        JSONObject json = new JSONObject();
        if(pageSize != null && pageNum != null){
            json.put("pageSize", pageSize);
            json.put("pageNum", pageNum);
        }
        if(deviceId!=null){
            json.put("deviceId",deviceId);
        }
        json.put("state",state);
        return ResultUtil.success( deviceCpeBusi.getCpeList(json));
    }
    /*查询单个cpe设备详情*/
    @GetMapping(value = "/cpe/{deviceId}")
    public Result<JSONObject> getCpeInfo(@PathVariable("deviceId") Long deviceId){
        return ResultUtil.success(deviceCpeBusi.getCpeInfo(deviceId));
    }
    /*修改cpe设备*/
    @PutMapping(value = "/cpe")
    public Result<Object> modifyCpe(@RequestBody JSONObject json,
                                           HttpServletRequest request) throws Exception {
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.put("userID",Long.parseLong(userID.toString()));
        }
        deviceCpeBusi.updateCpe(json);
        return ResultUtil.success();
    }

    @DeleteMapping(value = "/cpe/{deviceId}")
    public Result deleteCpe(HttpServletRequest request, @PathVariable("deviceId") Long deviceId){
        deviceCpeBusi.deleteCpeInfo(deviceId);
        return ResultUtil.success();
    }
    
  //导出CPEExcel
    @GetMapping(value = "/cpe/export")
    public Result<Object> exportCPEInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	String snNum=request.getParameter("snNum");
    	deviceCpeBusi.exportCPEInfo(snNum, response,true);
    	return ResultUtil.success();
    }
  //导出CPEExcel模板
    @GetMapping(value = "/cpe/exportTemplate")
    public Result<Object> exportCPETemplate(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	String snNum=request.getParameter("snNum");
    	deviceCpeBusi.exportCPEInfo(snNum, response,false);
    	return ResultUtil.success();
    }    
  //CPEExcel
    @PostMapping(value ="/cpe/import")
    public Result<?> importBrand(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws Exception{
    	deviceCpeBusi.importCPEInfo(file.getInputStream());
    	return ResultUtil.success();

    }
    

}
